'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker'], function ($) {
  return {
  
    init: function () {
      this.initPage();
      this.initSearch();
      this.bind();
      this.unbind();
      this.changeTrailer();
      this.declare();
      this.cancelDeclare();
      this.out();
    },
  
    initPage: function () {
      
      $('a.batch-submit').addClass('disabled');
  
      $(document).on('click', '#checkAll', function(e) {
        $('input[name="id"]').prop('checked', $('#checkAll').is(':checked'));
      });
      
      $(document).on('change', '#checkAll, input[name="id"]', function(e) {
        var ids = $('input[name="id"]:checked').val();
        if (ids) {
          $('a.batch-submit').removeClass('disabled');
        } else {
          $('a.batch-submit').addClass('disabled');
        }
      });
      
    },
    
    initSearch: function() {
      $(document).on('shown.bs.modal', '#search', function(e) {
      $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      });
    },
    
    bind: function() {
      
      var that = this;
      
      $(document).on('shown.bs.modal', '#bind', function(e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
          
        that.bindTip();
        
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      })
      
    },
    
    bindTip: function() {
      $(document).on('change', 'select[name="tractorId"]', function(e) {
        
        $('#tips').hide();
        
        var code = $('select[name="tractorId"] option:selected').data("code");
        if (code) {
          $('#tips').show();
        }
        
      });
    },
    
    unbind: function() {
      $(document).on('shown.bs.modal', '#unbind', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      })
    },
    
    changeTrailer: function() {
      
      var that = this;
      
      $(document).on('shown.bs.modal', '#change-trailer', function(e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        that.getRelationInfo();
          
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        });
        
      })
      
    },
    
    getRelationInfo: function() {
      $(document).on('change', 'select[name="trailerId"]', function(e) {
        
        $('#tips').hide();
        
        var bind = $('select[name="trailerId"] option:selected').data("bind");
        if (bind) {
          
          var trailerId = $('select[name="trailerId"] option:selected').val();
          
          $.ajax({
            url: window.__CTX__.root + "/vehicle-sources/query-trailer?trailerId=" + trailerId,
            type: "GET",
          }).done(function(data){
            data = typeof data === 'String' ? JSON.parse(data) : data
            var brandCNCode = '（' + data.data.brandCNCode + '）'
            $('#tips').show();
            $('.tractor').text(brandCNCode);
          }).fail(function() {
            $('#tips').show();
          })
          
        }
        
      });
    },
    
    declare: function() {
      $(document).on("shown.bs.modal", "#declare", function (e) {
        $(e.currentTarget).find("form").shiftform();
      });
    },
    
    cancelDeclare: function() {
      $(document).on("shown.bs.modal", "#cancel-declare", function (e) {
        $(e.currentTarget).find("form").shiftform();
      });
    },
    
    out: function () {
      $(document).on("shown.bs.modal", "#out", function (e) {
        $(e.currentTarget).find("form").shiftform()
      });
    }

  }
})